package arquitetura.view;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.Color;

import exceptions.GeralException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;

import arquitetura.fachada.*;
import to.Cliente;
import util.*;
import javax.swing.JFormattedTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.BevelBorder;

public class ViewClienteRec extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 177532010203673874L;
	private JPanel contentPane;
	private Fachada fachada;
	private Cliente cliente;	
	private JTextField txtNome;
	private JFormattedTextField ftxtCpf;
	private JTextField txtEndereco;
	private JFormattedTextField ftxtFoneRes;
	private JFormattedTextField ftxtFoneCel;
	private JTextField txtEmail;
	private JButton btnLocalizarDependente;
	private JCheckBox chckbxDependente;
	private static int codigo;
	public static boolean clienteSalvo = false;
	
	public static void exibeTela(int codigoCliente) {
		codigo = codigoCliente;		
		try {
			ViewClienteRec dialog = new ViewClienteRec();
			dialog.setLocationRelativeTo(null);
			dialog.setModal(true);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public ViewClienteRec() throws ParseException {
		fachada = Fachada.getInstancia();
		cliente = new Cliente();
		
		setTitle("Cadastro de Clientes");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 564, 352);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(null);
		panel.setBounds(10, 272, 532, 36);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					preencherDadosCliente();
					
					if (codigo == 0) {						
						clienteSalvo = fachada.incluirCliente(cliente);
							if (clienteSalvo)
								limparDadosDaTela();					
					} else {
						clienteSalvo = fachada.alterarCliente(cliente);
						if (clienteSalvo)
							dispose();
					}					
				} catch (GeralException e1) {
					JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnSalvar.setBounds(315, 7, 89, 23);
		panel.add(btnSalvar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancelar.setBounds(414, 7, 89, 23);
		panel.add(btnCancelar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_1.setBackground(new Color(240, 248, 255));
		panel_1.setBounds(10, 11, 532, 256);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(255, 255, 255));
		panel_2.setForeground(new Color(0, 0, 0));
		panel_2.setBorder(new LineBorder(new Color(176, 224, 230)));
		panel_2.setBounds(10, 11, 512, 162);
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel label = new JLabel("Nome");
		label.setBounds(10, 11, 46, 14);
		panel_2.add(label);
		
		txtNome = new JTextField();
		txtNome.setColumns(10);
		txtNome.setBounds(10, 29, 370, 20);
		txtNome.setDocument(new DocumentoCaixaAlta(60));
		panel_2.add(txtNome);
		
		JLabel label_1 = new JLabel("CPF");
		label_1.setBounds(387, 11, 46, 14);
		panel_2.add(label_1);		
		
		ftxtCpf = new JFormattedTextField(Formato.cpf());
		ftxtCpf.setBounds(387, 29, 115, 20);
		panel_2.add(ftxtCpf);
		
		JLabel label_2 = new JLabel("Endere\u00E7o");
		label_2.setBounds(10, 60, 83, 14);
		panel_2.add(label_2);
		
		txtEndereco = new JTextField();
		txtEndereco.setColumns(10);
		txtEndereco.setBounds(10, 78, 492, 20);
		txtEndereco.setDocument(new DocumentoCaixaAlta(100));
		panel_2.add(txtEndereco);
		
		JLabel label_3 = new JLabel("Telefone Residencial");
		label_3.setBounds(10, 109, 110, 14);
		panel_2.add(label_3);		
		
		ftxtFoneRes = new JFormattedTextField(Formato.telefone());
		ftxtFoneRes.setBounds(10, 127, 101, 20);
		panel_2.add(ftxtFoneRes);
		
		JLabel label_4 = new JLabel("Telefone Celular");
		label_4.setBounds(120, 109, 101, 14);
		panel_2.add(label_4);
		
		ftxtFoneCel = new JFormattedTextField(Formato.telefone());
		ftxtFoneCel.setBounds(120, 127, 101, 20);
		panel_2.add(ftxtFoneCel);
		
		JLabel label_5 = new JLabel("Email");
		label_5.setBounds(230, 109, 72, 14);
		panel_2.add(label_5);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(230, 127, 271, 20);
		txtEmail.setDocument(new DocumentoCaixaBaixa(60));
		panel_2.add(txtEmail);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(new Color(255, 255, 255));
		panel_3.setBorder(new LineBorder(new Color(176, 224, 230)));
		panel_3.setBounds(10, 184, 512, 60);
		panel_1.add(panel_3);
		panel_3.setLayout(null);
		
		chckbxDependente = new JCheckBox("\u00C9 Dependente de:");
		chckbxDependente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnLocalizarDependente.setEnabled(chckbxDependente.isSelected());
			}
		});
		chckbxDependente.setBackground(new Color(255, 255, 255));
		chckbxDependente.setBounds(10, 11, 124, 23);
		panel_3.add(chckbxDependente);
		
		JLabel label_6 = new JLabel("dependente");
		label_6.setForeground(Color.RED);
		label_6.setBounds(15, 37, 486, 14);
		panel_3.add(label_6);
		
		btnLocalizarDependente = new JButton("Localizar Dependente");
		btnLocalizarDependente.setEnabled(false);
		btnLocalizarDependente.setBounds(138, 11, 142, 23);
		panel_3.add(btnLocalizarDependente);		
		
		if (codigo > 0){
			try {
				cliente = fachada.consultarCliente(codigo);
				preencherControlesComDadosCliente();
			} catch (GeralException e1) {
				JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
			}			
		}
	}
	
	private void preencherDadosCliente(){
		String foneRes = Util.removerFormatacao(ftxtFoneRes.getText());
		String foneCel = Util.removerFormatacao(ftxtFoneCel.getText());
		
		cliente.setNome(txtNome.getText());
		cliente.setEndereco(txtEndereco.getText());
		cliente.setTelefoneResidencial(foneRes);
		cliente.setTelefoneCelular(foneCel);
		cliente.setEhDependente(chckbxDependente.isSelected());
		setCpf();
		setEmail();
	}
	
	private void setCpf(){
		String cpf = Util.removerFormatacao(ftxtCpf.getText());
		
		if (Util.ehUmTextoVazio(cpf))
			cliente.setCpf(null);
		else
			cliente.setCpf(cpf);
	}
	
	private void setEmail(){
		String email = txtEmail.getText();
		
		if (Util.ehUmTextoVazio(email))
			cliente.setEmail(null);
		else
			cliente.setEmail(email);
	}
	
	private void limparDadosDaTela(){
		txtNome.setText(null);
		ftxtCpf.setText(null);
		txtEndereco.setText(null);
		ftxtFoneRes.setText(null);
		ftxtFoneCel.setText(null);
		txtEmail.setText(null);
		chckbxDependente.setSelected(false);
		btnLocalizarDependente.setEnabled(false);
	}
	
	private void preencherControlesComDadosCliente(){		
		txtNome.setText(cliente.getNome());
		ftxtCpf.setText(cliente.getCpf());
		txtEndereco.setText(cliente.getEndereco());
		ftxtFoneRes.setText(cliente.getTelefoneResidencial());
		ftxtFoneCel.setText(cliente.getTelefoneCelular());
		txtEmail.setText(cliente.getEmail());
		chckbxDependente.setSelected(cliente.getEhDependente());
	}
}
